package com.yanqu.road.server.manager.activity.channelmusicroomrank;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.activity.channelmusicroomrank.ChannelMusicRoomRankConfig;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.bussiness.servercenter.ChannelActivityBussiness;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.logic.helper.CrossChannelHelper;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;


import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 跨域妙音坊的活动管理类
 */
public class CrossChannelMusicRoomRankActivityMgr extends TempCrossMgr {

    private static Map<Integer,ChannelMusicRoomRankActivity> activityMap = new ConcurrentHashMap<>();

    @Override
    public boolean removeExpireData() throws Exception {
        List<Integer> removeList = new ArrayList<>();
        for (Map.Entry<Integer,ChannelMusicRoomRankActivity> entry:activityMap.entrySet()){
            ChannelMusicRoomRankConfig config = entry.getValue().getConfig();
            if (config ==null){
                removeList.add(entry.getKey());
                continue;
            }
            ActivityInfo activityInfo = config.getActivityInfo();
            //如果过期三天了 移除吧
            if ((activityInfo.getEndShowTime() * DateHelper.SECOND_MILLIONS + 3 * DateHelper.DAY_MILLIONS) < System.currentTimeMillis()){
                removeList.add(entry.getKey());
                continue;
            }
        }
        for (Integer remove:removeList){
            activityMap.remove(remove);
            getLogger().info("跨域妙音坊冲榜跨服数据过期 活动id为{}",remove);
        }

        return true;
    }

    @Override
    public boolean save() {
        for (ChannelMusicRoomRankActivity activity : activityMap.values()){
            activity.save();
        }
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        reloadActivity();
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static synchronized void reloadActivity() {
        getLogger().info("reload Cross ChannelMusicRoomRankActivityMgr start");
        //只有链接跨域的活动跨服需要读取活动
        if (CrossChannelHelper.isCross3(CrossBaseServer.getInstance().getServerId())){
            // 先检查有没有数据库连接配置
            String dbParam = ChannelConfig.CHANNEL_CENTER_DB_PARAM;
            if ("0".equals(dbParam)) {
                getLogger().info("[ ChannelConfig.CHANNEL_CENTER_DB_PARAM ] is zero!");
                return;
            }
            Map<Integer, ActivityInfo> activityInfoMap = ChannelActivityBussiness.getOpeningActivityInfoMapByType(dbParam, ChannelConfig.CHANNEL_ACTIVITY_TIME_ZONE,eActivityType.ChannelMusicRoomRank.getValue(),1);
            if (activityInfoMap != null && !activityInfoMap.isEmpty()){
                for (ActivityInfo activityInfo : activityInfoMap.values()){
                    reloadActivity(activityInfo);
                    getLogger().info("reload Cross ChannelMusicRoomRankActivityMgr activity success activityId{}",activityInfo.getActivityId());
                }
            }
        }
        getLogger().info("reload Cross ChannelMusicRoomRankActivityMgr end");
    }

    public static void reloadActivity(ActivityInfo activityInfo) {
        if (activityInfo == null) {
            return;
        }
        int activityId = activityInfo.getActivityId();
        if (CrossChannelMusicRoomRankConfigMgr.getConfig(activityId) == null) {
            CrossChannelMusicRoomRankConfigMgr.reloadConfig(activityInfo);
        }

        ChannelMusicRoomRankActivity tempActivity = new ChannelMusicRoomRankActivity(activityInfo);
        activityMap.put(activityId, tempActivity);
    }

    /**
     * 拿活动
     * @param activityId
     * @return
     */

    public static ChannelMusicRoomRankActivity getActivity(int activityId) {
        ChannelMusicRoomRankActivity activity = activityMap.get(activityId);
        if (activity == null) {
            synchronized (activityMap) {
                activity = activityMap.get(activityId);
                if (activity == null) {
                    //如果不是跨域指定跨服 就是请求错了 不能加载出来
                    if (!CrossChannelHelper.isCross3(CrossBaseServer.getInstance().getServerId())){
                        getLogger().error("请求加载活动到非跨域指定活动跨服,加载失败,活动id{}",activityId);
                        return null;
                    }
                    // 先检查有没有数据库连接配置
                    String dbParam = ChannelConfig.CHANNEL_CENTER_DB_PARAM;
                    if ("0".equals(dbParam)) {
                        getLogger().info("[ ChannelConfig.CHANNEL_CENTER_DB_PARAM ] is zero!");
                        return null;
                    }
                    ActivityInfo activityInfo = ChannelActivityBussiness.getActivityInfoById(dbParam, activityId, ChannelConfig.CHANNEL_ACTIVITY_TIME_ZONE);
                    if (activityInfo == null) {
                        getLogger().error("获取跨域妙音坊冲榜活动失败,活动id{}",activityId);
                        return null;
                    }
                    if (CrossChannelMusicRoomRankConfigMgr.getConfig(activityId) == null) {
                        CrossChannelMusicRoomRankConfigMgr.reloadConfig(activityInfo);
                    }

                    activity = new ChannelMusicRoomRankActivity(activityInfo);
                    activityMap.put(activityId, activity);
                    getLogger().info("加载跨域妙音坊冲榜活动成功,活动id{}",activityId);
                }
            }
        }
        return activity;
    }

    /**
     * 定时任务
     */
    public static void activityTask() {
        for (ChannelMusicRoomRankActivity activity : activityMap.values()) {
            try {
                activity.settleTask();
            } catch (Exception e) {
                getLogger().error("activityTask error", e);
            }
        }
    }
}
